package com.yuexunit.finance.member.action.v1;


import java.io.BufferedInputStream; 
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;

import com.google.inject.Singleton;
import com.yuexunit.finance.member.action.v1.model.EnterpriseBaseDetail;
import com.yuexunit.inject.ComponentFactory;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.util.StringUtil;
import com.yuexunit.web.annotation.ActionServlet;
import com.yuexunit.web.annotation.Permission;

import net.sf.jxls.transformer.XLSTransformer;

/**
 *@version 创建时间：2017年6月27日 下午2:29:56
 *@author wenwenlong
 *类说明
 */

@ActionServlet({"/api/[^/]+/exportEnterpriseInfo.json"})
@Singleton
@Permission
public class ExportEnterpriseInfo extends HttpServlet{

	private static final long serialVersionUID = 1L;

	private EnterpriseActionApi enterpriseActionApi = ComponentFactory.loadClass(EnterpriseActionApi.class);

	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response){
		try {
			String et = request.getParameter("enterpriseType");
			Integer enterpriseType = null;
			if (StringUtil.isNotBlank(et)) {
				enterpriseType = Integer.valueOf(et);
			}

			PageCriteria page = PageCriteria.defaultInstance();
			page.setPageSize(1000);
			PageList<EnterpriseBaseDetail> ebds = enterpriseActionApi.inquireEnterprisePageList(null, null, null, null, null, null, null, enterpriseType, null, null, null, null, null, null, null, page);
			Map<String,Object> beans = new HashMap<String,Object>();  
			beans.put("arr", ebds);  
			XLSTransformer transformer = new XLSTransformer();  

			InputStream in=null;  
			OutputStream out=null;  
			//设置响应  
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(("客户信息.xls").getBytes("GB2312"), "ISO_8859_1"));  
			response.setContentType("application/vnd.ms-excel");

			in = new BufferedInputStream(new FileInputStream("C:\\Finance\\upload\\update\\enterprise_array.xls"));  
			Workbook workbook = transformer.transformXLS(in, beans);
			out=response.getOutputStream();  
			workbook.write(out);  
			out.flush();
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) {
		this.doGet(request, response);
	}

}
